<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Models\Shortlist;
use App\Models\Shortgroup;
use App\Models\Users;
use App\Models\Memberorder;
use App\Services\OutputService;
use Exception,Log,Auth;

class ShortController extends Controller
{
    public function userList(Request $request)
    {
        $users = Auth::user();
        $userLists = Users::adminUserList($request->code);
        $miniapps = ["wx830386602e6af5b1"=>"短网址生成","wx9a1e9abb406ac393"=>"短链接生成小助手"];
        $openids = $userLists->pluck("openid")->all();
        $memberOrders = Memberorder::userMenberLists($openids);
        foreach ($userLists as $userList) {
            $memberOrder = $memberOrders->where("openid",$userList->openid)->first();
            if (!empty($memberOrder)) {
                $userList->end_time = date("Y-m-d H:i:s",$memberOrder->end_time);
            } else {
                $userList->end_time = "-";
            }
        }
        return view("admin.manage.user_list",["userLists"=>$userLists,"miniapps"=>$miniapps]);
    }

    public function shortList(Request $request)
    {
        $users = Auth::user();
        $groupLists = Shortgroup::getShortGroup($users->openid);
        $shortLists = Shortlist::getUserShortList($users->openid,$request->name,$request->group_id);
        $condition = $request->all();
        return view("admin.short.list",["groupLists"=>$groupLists,"shortLists"=>$shortLists,"condition"=>$condition]);
    }

    public function memberList(Request $request)
    {
        $users = Auth::user();
        $memberOrders = Memberorder::adminMemberLists(2);
        $condition = $request->all();
        $miniapps = ["wx830386602e6af5b1"=>"短网址生成","wx9a1e9abb406ac393"=>"短链接生成小助手"];
        return view("admin.manage.member_list",["memberOrders"=>$memberOrders,"condition"=>$condition,"miniapps"=>$miniapps]);
    }

    public function shortAdd(Request $request)
    {
        $users = Auth::user();
        $groupLists = Shortgroup::getShortGroup($users->openid);
        return view("admin.short.short_add",["groupLists"=>$groupLists]);
    }

    public function shortUpdate(Request $request,$short_id)
    {
        $users = Auth::user();
        $shortDetial = Shortlist::shortDetial($users->openid,$short_id);
        return view("admin.short.short_update",["shortDetial"=>$shortDetial]);
    }

    public function shortDetial(Request $request,$short_id)
    {
        $users = Auth::user();
        $shortDetial = Shortlist::shortDetial($users->openid,$short_id);
        return view("admin.short.short_detial",["shortDetial"=>$shortDetial]);
    }

    public function allShortList(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 2) return redirect("/admin/short/list");
        $shortLists = Shortlist::allShortList();
        return view("admin.manage.short_list",["shortLists"=>$shortLists]);
    }

    public function userDisable(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 2) return OutputService::error("您无权执行本操作!");
        Users::updateUserDisable($request->openid,2);
        Shortlist::userShortListDel($request->openid);
        return OutputService::success("删除成功!");
    }
}